#!/usr/bin/env python
# encoding: utf-8

from PyQt5.QtCore import QTimer,Qt,QPointF,QRectF
from PyQt5.QtGui import QPainter,QColor,QRadialGradient,QPen,QConicalGradient,QBrush,QPalette,QFont
from PyQt5.QtWidgets import QWidget,QApplication,QLabel,QHBoxLayout,QPushButton,QDialog


class chartWidget(QDialog):
    def __init__(self, parent=None):
        super(chartWidget, self).__init__()
        self.setMinimumSize(167, 167)
        self.setFixedSize(200, 200)
        self.setWindowTitle('漏射线测量')
        #  self.setWindowIcon('app.ico')
        self.setAutoFillBackground(True)
        palette = self.palette()
        palette.setColor(QPalette.Background, QColor(255, 255, 255))
        #  self.setPalette(palette)
        self.setWindowFlags(Qt.FramelessWindowHint)# 无窗体
        self.setAttribute(Qt.WA_TranslucentBackground)# 背景透明

        # self.m_updateTimer = QTimer()
        # self.m_updateTimer.setInterval(100)
        # self.m_updateTimer.timeout.connect(self.updataAngle)
        # self.m_updateTimer.start()

        self.maxValue = 100
        self.minValue = 0
        self.currentValue= 0

        self.usedColor          = QColor(165,    220, 62)
        self.freeColor          = QColor(215,    215, 215)
        self.rangeTextColor     = QColor(137,    137, 137)
        self.valueTextColor     = QColor(52,     155, 218)
        self.valueBgColor       = QColor(239,    239, 239)
        self.outBgColor         = QColor(233,    233, 233)
        self.centerBgColorStart = QColor(45,     204, 112)
        self.centerBgColorEnd   = QColor(51,     152, 219)
        self.rangeTextFont      = QFont("Arial", 10, QFont.Normal)
        self.valueTextFont      = QFont("Arial", 12, QFont.Bold)
        self.currentText = ''
        self.unit ='%'

        self.m_angle = 0
        self.m_outerRadius = 149

        self.m_color= QColor(Qt.gray)
        self.m_rotateAngle = 0
        self.m_clockwiseRotate = True
        self.m_rotateDelta = 36

    def paintEvent(self,e):
        painter = QPainter()
        painter.begin(self)
        painter.setRenderHints(QPainter.Antialiasing|QPainter.HighQualityAntialiasing)#设置反锯齿
        # self.drawLineWait(painter)
        # self.drawPie(painter)#画仪表盘
        self.drawPieEnv(painter)#画仪表盘
        painter.end()

    def drawPie(self,painter):
        if self.width() > self.height() :
            diameter = self.height()
        else:
            diameter = self.width()
        width= self.width()
        height= self.height()
        #绘制准备工作,旋转坐标轴中心
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        painter.translate(width / 2, height / 2)
        painter.save()
        painter.restore()
        painter.save()
        painter.rotate(40)
        #自动计算比例关系
        percent = 100 / (self.maxValue - self.minValue)
        currentPercent = int(self.currentValue * percent)
        maxPercent = (self.maxValue * percent)
        #自动计算位置比例
        diameterLine = (3 / 167) * diameter
        diameterWidth = (70 / 167) * diameter
        diameterHeight = (80 / 167) * diameter
        diameter1 = (55 / 167) * diameter
        diameter2 = (35 / 167) * diameter
        diameter3 = (30 / 167) * diameter
        diameter4 = (80 / 167) * diameter
        diameter5 = (135 / 167) * diameter

        painter.setPen(QPen(self.usedColor, diameterLine))
        for i in range(int(self.minValue),int(currentPercent)):
            painter.drawLine(0, diameterWidth, 0, diameterHeight)
            painter.rotate(2.8)

        painter.setPen(QPen(self.freeColor, diameterLine))
        for i in range(int(currentPercent),int(maxPercent)):
            painter.drawLine(0, diameterWidth, 0, diameterHeight)
            painter.rotate(2.8)
        painter.restore()

        #绘制最外框圆形背景
        painter.translate(-width / 2, -height / 2)
        painter.setBrush(self.outBgColor)
        painter.setPen(QPen(self.outBgColor, 15))
        painter.drawEllipse(QRectF((width / 2 - diameter1), (height / 2 - diameter1), diameter1 * 2, diameter1 * 2))

        #绘制中间框圆形渐变背景
        conicalGradient=QConicalGradient(width / 2, height / 2, 90)
        conicalGradient.setColorAt(0, self.centerBgColorStart)
        conicalGradient.setColorAt(1.0, self.centerBgColorEnd)
        painter.setPen(QPen(QBrush(conicalGradient), 30))
        painter.drawEllipse(QRectF((width / 2 - diameter2), (height / 2 - diameter2), diameter2 * 2, diameter2 * 2))
        #绘制最里框百分比文字圆形背景
        painter.setPen(Qt.NoPen)
        painter.setBrush(self.valueBgColor)
        painter.drawEllipse(QRectF((width / 2 - diameter3), (height / 2 - diameter3), diameter3 * 2, diameter3 * 2))
        #绘制百分比文字及范围值文字
        painter.setPen(QPen(self.valueTextColor))
        painter.setFont(self.valueTextFont)
        painter.drawText(0, 0, width, height, Qt.AlignCenter, '%s%s'%(self.currentValue,self.unit))
        painter.setPen(QPen(self.rangeTextColor))
        painter.setFont(self.rangeTextFont)
        painter.drawText(0, 0, width - diameter4, height + diameter5, Qt.AlignCenter, ("%s%s")%(self.minValue,self.unit))
        painter.drawText(0, 0, width + diameter4, height + diameter5, Qt.AlignCenter, ("%s%s")%(self.maxValue,self.unit))

    def drawLineWait(self, p):
        # QPainter p(this);
        p.setRenderHint(QPainter.Antialiasing);
        p.translate(self.width()/2, self.height()/2);
        side = min(self.width(),self.height())
        p.scale(side/100.0, side/100.0);

        p.rotate(self.m_rotateAngle);
        # self.drawLineWait(p);
        if self.m_clockwiseRotate:
            self.m_rotateAngle = self.m_rotateAngle +self.m_rotateDelta
        else:
            self.m_rotateAngle = self.m_rotateAngle -self.m_rotateDelta
        self.m_rotateAngle %= 360;

        #根据100*100的大小进行画,在paintEvent中进行过缩放
        p.save();
        p.setPen(Qt.NoPen);
        color = self.m_color
        for i in range(0,10):
            color.setAlphaF(1.0*i/10);
            p.setBrush(QBrush(color));
            p.drawRoundedRect(50*2/5, -3, 50*3/5, 6, 2, 2);
            p.rotate(36);
        p.restore();

        #旋转坐标轴
        width= self.width()/2
        height= self.height()/2
        p.translate(-width / 2, -height / 2)
        #绘制百分比文字及范围值文字
        p.setPen(QPen(self.valueTextColor))
        p.setFont(self.valueTextFont)
        p.drawText(0, 0, width, height, Qt.AlignCenter, '%s%s'%('sdfsddfs',self.unit))

        # p.setPen(QPen(self.rangeTextColor))
        # p.setFont(self.rangeTextFont)
        # p.drawText(0, 0, width - diameter4, height + diameter5, Qt.AlignCenter, ("%s%s")%(self.minValue,self.unit))
        # p.drawText(0, 0, width + diameter4, height + diameter5, Qt.AlignCenter, ("%s%s")%(self.maxValue,self.unit))

    def drawPieEnv(self,painter):
        if self.width() > self.height() :
            diameter = self.height()
        else:
            diameter = self.width()
        width= self.width()
        height= self.height()
        #绘制准备工作,旋转坐标轴中心
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        painter.translate(width / 2, height / 2)
        painter.save()
        painter.restore()
        painter.save()
        painter.rotate(40)
        #自动计算比例关系
        percent = 100 / (self.maxValue - self.minValue)
        currentPercent = int(self.currentValue * percent)
        maxPercent = (self.maxValue * percent)
        #自动计算位置比例
        diameterLine = (3 / 167) * diameter
        diameterWidth = (70 / 167) * diameter
        diameterHeight = (80 / 167) * diameter
        diameter1 = (55 / 167) * diameter
        diameter2 = (35 / 167) * diameter
        diameter3 = (30 / 167) * diameter
        diameter4 = (80 / 167) * diameter
        diameter5 = (135 / 167) * diameter

        painter.setPen(QPen(self.usedColor, diameterLine))
        for i in range(int(self.minValue),int(currentPercent)):
            painter.drawLine(0, diameterWidth, 0, diameterHeight)
            painter.rotate(2.8)

        painter.setPen(QPen(self.freeColor, diameterLine))
        for i in range(int(currentPercent),int(maxPercent)):
            painter.drawLine(0, diameterWidth, 0, diameterHeight)
            painter.rotate(2.8)
        painter.restore()

        #移动坐标轴
        painter.translate(-width / 2, -height / 2)
        #绘制最外框圆形背景
        # painter.setBrush(self.outBgColor)
        # painter.setPen(QPen(self.outBgColor, 15))
        # painter.drawEllipse(QRectF((width / 2 - diameter1), (height / 2 - diameter1), diameter1 * 2, diameter1 * 2))

        #绘制中间框圆形渐变背景
        # conicalGradient=QConicalGradient(width / 2, height / 2, 90)
        # conicalGradient.setColorAt(0, self.centerBgColorStart)
        # conicalGradient.setColorAt(1.0, self.centerBgColorEnd)
        # painter.setPen(QPen(QBrush(conicalGradient), 30))
        # painter.drawEllipse(QRectF((width / 2 - diameter2), (height / 2 - diameter2), diameter2 * 2, diameter2 * 2))
        #绘制最里框百分比文字圆形背景
        # painter.setPen(Qt.NoPen)
        # painter.setBrush(self.valueBgColor)
        # painter.drawEllipse(QRectF((width / 2 - diameter3), (height / 2 - diameter3), diameter3 * 2, diameter3 * 2))
        #绘制百分比文字及范围值文字
        painter.setPen(QPen(self.valueTextColor))
        painter.setFont(self.valueTextFont)
        painter.drawText(0, 0, width, height, Qt.AlignCenter, '%s\r\n%s%s'%(self.currentText,self.currentValue,self.unit))
        # painter.setPen(QPen(self.rangeTextColor))
        # painter.setFont(self.rangeTextFont)
        # painter.drawText(0, 0, width - diameter4, height + diameter5, Qt.AlignCenter, ("%s%s")%(self.minValue,self.unit))
        # painter.drawText(0, 0, width + diameter4, height + diameter5, Qt.AlignCenter, ("%s%s")%(self.maxValue,self.unit))

    def updataAngle(self):
        # self.m_outerRadius = self.m_outerRadius -1
        self.currentValue=self.currentValue+9
        self.m_angle=self.m_angle+6
        # self.m_angle=m_angle+36
        if self.m_angle >360:
            self.m_angle = 0
        # self.currentValue=10
        if self.currentValue >self.maxValue:
            self.currentValue = 0
        self.update()

    def update_env(self,t):
        self.currentText = '漏射线测量'
        self.currentValue = t
        # float('%0.1f'%(100-t*100/30))
        self.update()

    def reject(self):
        QDialog.reject(self)


if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    window = chartWidget()
    window.show()
    window.resize(100, 100)
    app.exec_()
    # sys.exit(app.exec_())
